<?php

namespace App\Admin\Controllers;

use App\Models\UserScoreLog;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ScorelogsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '积分记录';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserScoreLog);

        $grid->column('id', __('Id'));
        $grid->column('user_id', __('用户'))->display(function(){
            return User::getMobileById($this->user_id);
        });
        $grid->column('before', __('变动前'));
        $grid->column('change', __('变动'))->display(function(){
            $signStr = $this->type == 2?'-':'+';
            return $signStr.$this->change;
        });
        $grid->column('after', __('变动后'));
        $grid->column('desc', __('说明'));
        $grid->column('created_at', __('操作时间'));
        $grid->disableActions();
        $grid->model()->orderBy('id','desc');
        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->where(function($query){
                $query->whereHas('users',function($query){
                    $query->where('mobile','=',$this->input);
                });
            },'手机号');
        });
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->disableExport();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserScoreLog::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('before', __('Before'));
        $show->field('change', __('Change'));
        $show->field('after', __('After'));
        $show->field('type', __('Type'));
        $show->field('desc', __('Desc'));
        $show->field('link_id', __('Link id'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserScoreLog);

        $form->number('user_id', __('User id'));
        $form->number('before', __('Before'));
        $form->number('change', __('Change'));
        $form->number('after', __('After'));
        $form->switch('type', __('Type'))->default(1);
        $form->text('desc', __('Desc'));
        $form->text('link_id', __('Link id'));

        return $form;
    }
}
